using System;
using Headsight.BPMN2WF.BPMN.Model;
using Headsight.BPMN2WF.Testing.TestObjCtors;
using Headsight.BPMN2WF.Transformation.Transformers.Additions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;

namespace Headsight.BPMN2WF.Transformation.Transformers.NoGoodName
{
    [TestClass]
    public class BPMN2WFTransformationExceptionHandlerTest : BPMN2WFTransformationDelegateBaseImplTestFixture<BPMN2WFTransformationExceptionHandler>
    {
        private BPMNFlowNode _flowNode;
        private BPMNTransformationResult _result;

        protected override BPMN2WFTransformationExceptionHandler CreateSut(IBPMN2WFTransformation @delegate)
        {
            _ctx = new BPMN2WFTransformationContextCtor().Create();
            _flowNode = new BPMNFlowNodeCtor().Create();

            _result = new BPMNTransformationResultCtor().Create();

            return new BPMN2WFTransformationExceptionHandler(@delegate);
        }

        [TestMethod]
        public void Transform()
        {
            _delegate.Expect(x => x.Transform(_ctx, _flowNode)).Return(_result);

            Assert.AreSame(_result, _sut.Transform(_ctx, _flowNode));
        }

        [TestMethod]
        public void Transform_in_case_of_delegate_throwing_exception()
        {
            var exception = new Exception();

            _flowNode = new BPMNFlowNodeCtor().WithId("id123").Create();
            _delegate.Expect(x => x.Transform(_ctx, _flowNode)).Throw(exception);

            try
            {
                _sut.Transform(_ctx, _flowNode);
                Assert.Fail("**failed**");
            }
            catch (Exception e)
            {
                const String exp = "BPMNFlowNode[Id: id123, Name: ] / BPMNFlowNode[Id: id123, Name: ] / ";
                Assert.AreEqual(exp, e.Message);
                Assert.AreSame(exception, e.InnerException);
            }
        }
    }
}